﻿namespace etracks.Web.Controllers
{
    using etracks.EntityModels;
    using etracks.Helpers;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Mvc.Ajax;
    using System.Data.Linq;
    using System.Data.Common;

    /// <summary>
    /// Controller for managing environment resource
    /// </summary>
	[Authorize]
	public class EnvironmentResourceController : BaseController
	{
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">Environment id</param>
        /// <returns>List View</returns>
        public ActionResult List(int id)
        {
            EnvironmentResourceListViewModel vm = new EnvironmentResourceListViewModel();

            etracks.EntityModels.Environment e = EnvironmentResourceController.GetEnvironment(id);
            vm.SetValuesFrom(e);
            vm.ResourceList = GetResourceList(e.Id);

            return View(vm);

        } // public ActionResult List()

        ////////////////////////////////////////////////////////////////////////////////
        // Data retrieval hack functions
        private static etracks.EntityModels.Environment GetEnvironment(int id)
        {
            etracks.EntityModels.Environment result = new etracks.EntityModels.Environment();

            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("id", id);

            DataTable dt = PostgresHelper.GetDataTable("SELECT * FROM Environment WHERE id = :id ORDER BY id LIMIT 1;", parameters);

            if (dt != null)
            {
                result = dt.AsEnumerable().Select(n => new etracks.EntityModels.Environment
                {
                    Id = n.Field<int>("id"),
                    Name = n.Field<string>("name"),
                    ProjectId = n.Field<int?>("project_id")
                }).FirstOrDefault();
            }

            return result;
        }

        private static List<etracks.Web.ResourceReadOnlyViewModel> GetResourceList(int id)
        {
            //List<etracks.Web.ResourceReadOnlyViewModel>
            List<etracks.Web.ResourceReadOnlyViewModel> result = new List<etracks.Web.ResourceReadOnlyViewModel>();

            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("environment_id", id);

            DataTable dt = PostgresHelper.GetDataTable(@"SELECT  r.*, rt.name AS ""resource_type_name""
FROM    environment_resource er
INNER JOIN
        resource r
        ON er.resource_id = r.id
INNER JOIN
        resource_type rt
        ON r.resource_type_id = rt.id
WHERE environment_id = :environment_id;", parameters);

            if (dt != null)
            {
                result = dt.AsEnumerable().Select(n => new etracks.Web.ResourceReadOnlyViewModel
                {
                    Id = n.Field<int>("id"),
                    Name = n.Field<string>("name"),
                    Alias = n.Field<string>("alias"),
                    ResourceType = n.Field<string>("resource_type_name")
                }).ToList();
            }

            return result;
        }

    } // public class EnvironmentResourceController : BaseController

} // namespace etracks.Web.Controllers